package com.yun.algorithm.dynamicprogramming;

import java.util.Arrays;

/**
 * 动态规划
 * 最长公共子串
 */
public class LCSubstring {

    public static void main(String[] args) {
        System.out.println(lcs("itheima", "thiema"));
    }

    static int lcs(String a, String b) {
        char[] cha = a.toCharArray();
        char[] chb = b.toCharArray();
        int[][] dp = new int[cha.length][chb.length];
        int max = 0;
        for (int i = 0; i < cha.length; i++) {
            for (int j = 0; j < chb.length; j++) {
                if (cha[i] == chb[j]) {
                    if (i == 0 || j == 0) {
                        dp[i][j] = 1;
                    } else {
                        dp[i][j] = dp[i - 1][j - 1] + 1;
                    }
                }
                max = Math.max(max, dp[i][j]);
            }
            System.out.println(Arrays.toString(dp[i]));
        }
        return max;
    }
}
